package com.crm.controller;

import com.crm.client.MemberClient;
import com.crm.entity.LayPage;
import com.crm.entity.Member;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;


@Controller
@RequestMapping
public class MemberCotroller {

    @Autowired
    MemberClient memberClient;

    @RequestMapping("/toMemberList")
    public String memberListPage(){
        return "member/memberList";
    }
    @RequestMapping("/toMemberEdit")
    public String memberEditPage(){
        return "member/memberEdit";
    }
    @GetMapping("/members")
    @ResponseBody
    public LayPage<Member> memberList(int page,int limit, String key){
        if(key==null){key="";}
        LayPage<Member> pdata = memberClient.getMembers(page-1,limit,key,key,key);
        return pdata;
    }

    @RequestMapping("/member/add")
    @ResponseBody
    public Map<String,Object> addMember(Member member){
        Map<String,Object> result = new HashMap<String,Object>();
        try{
            member.setCreateDate(new Date());
            member = memberClient.post(member);
            result.put("code",0);
            result.put("member",member);
        }catch (Exception e){
            e.printStackTrace();
            result.put("code",1);
        }
        return result;
    }
}
